var request = null;
var blogid = null;
var replierName = null;
var replierEmail = null;
var verifyCode = null;
var content = null;

function createXMLHttpRequest() {
	if (window.XMLHttpRequest) {
		// Mozilla
		request = new XMLHttpRequest();
	} else if (window.ActionXObject) {
		// IE
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			}
		}
	}
}


function commitReply(){
	createXMLHttpRequest();
	
	//从页面获取数据
	blogid = document.getElementById("blogid").value;
	replierName = document.getElementById("replierName").value;
	replierEmail = document.getElementById("replierEmail").value;
	verifyCode = document.getElementById("code").value;
	content = document.getElementById("content").value;

	//将数据整合，这样有利于在action中获取
	var postContent = "blogid=" + blogid + "&replierName=" + encodeURIComponent(replierName)
			+ "&replierEmail=" + encodeURIComponent(replierEmail)
			+ "&verifyCode=" + encodeURIComponent(verifyCode) + "&content="
			+ encodeURIComponent(content);

	request.open("POST", "commitReply", true);
	request.onreadystatechange = callback;
	request.setRequestHeader("Content-Type",
	"application/x-www-form-urlencoded");
	request.send(postContent);

	
}

function callback(){
	if (request.readyState == 4){
		if (request.status == 200){
			if (request.responseText == "1"){
				//1代表验证码不正确
				document.getElementById("verifyMessage").innerHTML = "验证码错误";
			} else {
				document.getElementById("comment-entry").innerHTML = document
						.getElementById("comment-entry").innerHTML
						+ request.responseText;
				document.getElementById("commentform").reset();
				document.getElementById("verifyMessage").innerHTML = null;
				reloadVerifyImage();
			}
		}
	}
}


//当点击验证码之后，重新生成验证码
function reloadVerifyImage(){
	var time = new Date().getTime();//防止浏览器缓存
	document.getElementById("verifyImage").src = "generateVerifyImage?t=" + time;
}
